"""
Case Type   : 功能测试
Case Name   : 集群带业务，集群触发failover
Create At   : 2025/03/25
Owner       : @chen
Description :
    1.拉起TPCC业务
./runBenchmark.sh pri_props.og
./runBenchmark.sh slave_props.og
    2.主节点故障：
mv dn1 dn
kill -9 pid
    3.查看集群状态,查看业务
    4.恢复环境：
mv dn dn1
cm_ctl start -n 1
cm_ctl switchover -a
Expect      :
    1.成功
    2.成功
    3.正常
    4.成功
History     :
"""

import unittest
import os
import time
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread
from yat.test import Node
from yat.test import macro
logger = Logger()

class Test_Xlog_Syncretism_0012(unittest.TestCase):
    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.pri_node = Node('PrimaryDbUser')
        self.sta1_node = Node('Standby1DbUser')
        self.sta2_node = Node('Standby2DbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.tpcc_node = Node('Standby2Root')
        self.constant = Constant()
        self.com = Common()
        self.pri_tpcc_log = 'pri_tpcc_result.log'
        self.sta_tpcc_log = 'sta_tpcc_result.log'
    
    def test_shared_storage(self):
        text = '-----step1:拉起TPCC业务 expect:成功-----'
        logger.info(text)
        logger.info('-----主节点读写业务-----')
        pri_tpcc = ComThread(self.com.startTPCC(), args=(self.tpcc_node, f'{macro.PRI_PROPS_30}', self.pri_tpcc_log))
        pri_tpcc.setDaemon(True)
        pri_tpcc.start()
        logger.info('-----备节点读写业务-----')
        sta_tpcc = ComThread(self.com.startTPCC(), args=(self.tpcc_node, f'{macro.STA_PROPS_30}', self.sta_tpcc_log))
        sta_tpcc.setDaemon(True)
        sta_tpcc.start()

        text = '-----step2:主节点故障 expect:成功-----'
        logger.info(text)
        cmd = f'mv {macro.DB_INSTANCE_PATH} {macro.DB_INSTANCE_PATH}_bak'
        logger.info(cmd)
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        pid = self.com.get_pid(self.pri_node, 'bin/gaussdb')
        logger.info(pid)
        cmd = f"kill -9 {pid}"
        logger.info(cmd)
        res = self.pri_node.sh(cmd).result()
        logger.info(res)

        text = '-----step3:查看集群状态,查看业务 expect:成功-----'
        while True:
            status = self.pri_sh.exec_cm_ctl('query', '-Cv')
            if "Primary Normal" in status and 'Down    Manually stopped' in status:
                logger.info(status)
                break
            time.sleep(5)
        pri_cmd = f'cd {macro.TPCC_PATH} && tail -n 1 {self.pri_tpcc_log}'
        sta_cmd = f'cd {macro.TPCC_PATH} && tail -n 1 {self.sta_tpcc_log}'
        logger.info(pri_cmd)
        pri_res = self.tpcc_node.sh(pri_cmd).result()
        logger.info(pri_res)
        self.assertIn('Current tmpTOTAL', pri_res, 'tpcc业务执行失败')
        logger.info(sta_cmd)
        sta_res = self.tpcc_node.sh(sta_cmd).result()
        logger.info(sta_res)
        self.assertIn('Current tmpTOTAL', pri_res, 'tpcc业务执行失败')
    
    def tearDown(self):
        text = '-----step4:恢复环境 expect:成功-----'
        logger.info(text)
        logger.info('-----停tpcc线程-----')
        pri_tpcc = self.com.get_pid(self.tpcc_node, f"ps -ux | grep '{macro.PRI_PROPS_30}'" \
            f" | grep -v '--color")
        sta_tpcc = self.com.get_pid(self.tpcc_node, f"ps -ux | grep '{macro.STA_PROPS_30}'" \
            f" | grep -v '--color")
        logger.info(pri_tpcc)
        cmd = f"kill -9 {pri_tpcc}"
        logger.info(cmd)
        res = self.tpcc_node.sh(cmd).result()
        logger.info(res)
        logger.info(sta_tpcc)
        cmd = f"kill -9 {sta_tpcc}"
        logger.info(cmd)
        res = self.tpcc_node.sh(cmd).result()
        logger.info(res)
        logger.info('-----恢复主节点故障-----')
        cmd = f'mv {macro.DB_INSTANCE_PATH}_bak {macro.DB_INSTANCE_PATH}'
        logger.info(cmd)
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        res = self.pri_sh.exec_cm_ctl('start', '-n 1')
        logger.info(res)
        self.assertIn(self.constant.cm_start_node_success_msg, res)
        res = self.pri_sh.exec_cm_ctl('switchover', '-a')
        logger.info(res)
        self.assertIn(self.constant.cm_switchover_success_msg, res)
        logger.info(f'-----{os.path.basename(__file__)} end-----')